using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.UI;

namespace PHSRAG.Insight21.InsightWebControls
{
	#region Tab Class
	//Single Tab Class

	public class Tab 
	{
		#region Constructors
		public Tab() 
		{
			//Default Constructor
		}

		public Tab(string strID) 
		{
			id = strID;
		}

		public Tab(string strID, string strName) 
		{
			id = strID;
			dispName = strName;
		}

		public Tab(string strID, string strName, int intWeight) 
		{
			id = strID;
			dispName = strName;
			weight = intWeight;
		}

		public Tab(string strName, int intWeight, string strPanel) 
		{
			dispName = strName;
			weight = intWeight;
			panel = strPanel;
		}

		#endregion
		#region Tab Members & Properties
		string id = "";
		bool enabled = true;
		string dispName = "";
		int weight;
		string panel = "";
		string url = "";

		public string ID 
		{
			get { return id; }
			set { id = value; }
		}

		public bool Enabled
		{
			get { return enabled; }
			set { enabled = value; }
		}

		public string DisplayName 
		{
			get { return dispName; }
			set { dispName = value; }
		}

		public int DisplayPosition 
		{
			get { return weight; }
			set { weight = value; }
		}

		public string LinkedToPanel 
		{
			get { return panel; }
			set { panel = value; }
		}

		public string URL 
		{
			get {return url; }
			set { url = value; }
		}
		#endregion
	}

	#endregion
	#region TabList Class

   public class TabList : Control, INamingContainer
   {
      protected class TabCompare : IComparer 
      {
         public int Compare(object x, object y) 
         {
            Tab tx = (Tab)x;
            Tab ty = (Tab)y;
            if (tx.DisplayPosition > ty.DisplayPosition) 
            {
               return 1;
            }
            else if (tx.DisplayPosition == ty.DisplayPosition) 
            {
               return 0;
            }
            else 
            {
               return -1;
            }
         }
      }

      public TabList()
      {
         //
         // TODO: Add constructor logic here
         //
      }

      #region Members & Properties
      enum tabSelType 
      {
         ID,
         DisplayName,
         Weight
      }

      bool bolUseFrames = true;
		
      string strTargetFrame = "";
      string strLeftImage = "";
      string strRightImage = "";
      string strTopImage = "";
      string strBottomImage = "";
      string strSelLeftImage = "";
      string strSelRightImage = "";
      string strSelTopImage = "";
      string strSelBottomImage = "";
      string cssClass = "";
      string SelCssClass = "";
      string disabledCssClass = "";
		
      int intSelTab;
      int intTabsAcross = 5;

      ArrayList items = new ArrayList();

      public Tab this[int index] 
      {
         get
         {
            return (Tab)items[index];
         }
      }

		public bool UseFrames 
		{
			get { return bolUseFrames; }
			set { bolUseFrames = value; }
		}

		public string TargetFrame 
		{
			get { return strTargetFrame; }
			set { strTargetFrame = value; }
		}
		public string LeftImage 
		{
			get { return strLeftImage; }
			set { strLeftImage = value; }
		}

		public string RightImage 
		{
			get { return strRightImage; }
			set { strRightImage = value; }
		}
		public string TopImage 
		{
			get { return strTopImage; }
			set { strTopImage = value; }
		}
		public string BottomImage 
		{
			get { return strBottomImage; }
			set { strBottomImage = value; }
		}
		public string LeftImageOver 
		{
			get { return strSelLeftImage; }
			set { strSelLeftImage = value; }
		}
		public string RightImageOver 
		{
			get { return strSelRightImage; }
			set { strSelRightImage = value; }
		}
		public string TopImageOver 
		{
			get { return strSelTopImage; }
			set { strSelTopImage = value; }
		}
		public string BottomImageOver 
		{
			get { return strSelBottomImage; }
			set { strSelBottomImage = value; }
		}


		public string CssClass 
		{
			get { return cssClass; }
			set { cssClass = value; }
		}
		public string CssClassOver 
		{
			get { return SelCssClass; }
			set { SelCssClass = value; }
		}

		public string DisabledCssClass
		{
			get { return disabledCssClass; }
			set { disabledCssClass = value; }
		}

		public int SelectedTab 
		{
			get { return intSelTab; }
			set { intSelTab = value; }
		}
		public int TabsAcross 
		{
			get { return intTabsAcross; }
			set { intTabsAcross = value; }
		}
		#endregion
		#region Methods
		//Adds an item to the List
		public void AddTab(Tab myTab) 
		{
			items.Add(myTab);
		}
		//Sorts the items in the list into display order
		private void order() 
		{
			items.Sort(new TabCompare());
		}

		//Returns the colspan for each tab
		private int GetTabColspan() 
		{
			int intColspan = 1;
			if (strLeftImage.Length > 0) 
			{ intColspan++; }
			if (strRightImage.Length > 0)
			{ intColspan++; }
			return intColspan;
		}

		//Returns render string for top image row 
		private string TopRow(int intStart,ref int intFinish, int intSelected) 
		{
			//intStart is the first tab index to display
			//intFinish is the last tab index displayed
			//intSelected is the currently selected tab

			int intTabspan = 0;
			string strReturn = "<tr>";

			//Set how many tabs to go across the page
			if (intTabsAcross <= items.Count) 
			{ intTabspan = intTabsAcross; }
			else { intTabspan = items.Count; }

			//Build up display string
			for (int i=0; i < intTabspan; ++i) 
			{
				if (items.Count > (intStart+i)) 
				{
					Tab myTab = (Tab)items[i+intStart];

					string topImage = (intSelected == myTab.DisplayPosition) ? strSelTopImage : strTopImage;

					strReturn += "<td colspan=\"" + GetTabColspan().ToString() + "\" class=\"" + cssClass + "\">";
					strReturn += "<img border=\"0\" src=\"" + topImage + "\">";
					strReturn += "</td>";
				}
				intFinish++;
			}

			strReturn += "</tr>";
			return strReturn;
		}

		//Returns render string for content row
		private void MiddleRow(int intStart,ref int intFinish, int intSelected) 
		{
			//intStart is the first tab index to display
			//intFinish is the last tab index displayed
			//intSelected is the currently selected tab

			int intTabspan = 0;
			this.Controls.Add(new LiteralControl("<tr>"));

			//Set how many tabs to go across the page
			if (intTabsAcross <= items.Count) 
			{ intTabspan = intTabsAcross; }
			else { intTabspan = items.Count; }

			//Build up display string
			for (int i=0; i < intTabspan; ++i) 
			{
				if (items.Count > (intStart+i)) 
				{
					Tab myTab = (Tab)items[i+intStart];

					string tdCssClass = (intSelected == myTab.DisplayPosition) ? SelCssClass : CssClass;
					string spanCssClass = (intSelected == myTab.DisplayPosition) ? "TabSelLink" : "TabLink";
               spanCssClass = myTab.Enabled ? spanCssClass : "TabDisabled";
					string leftImage = (intSelected == myTab.DisplayPosition) ? strSelLeftImage : strLeftImage;
					string rightImage = (intSelected == myTab.DisplayPosition) ? strSelRightImage : strRightImage;

					//Left Image if necessary
					if (GetTabColspan() > 1) 
					{
						this.Controls.Add(new LiteralControl("<td class=\""	+ tdCssClass + "\"><img src=\"" + leftImage + "\"></td>"));
					}

					System.Web.UI.HtmlControls.HtmlGenericControl btnLink = new System.Web.UI.HtmlControls.HtmlGenericControl();
					btnLink.InnerText = myTab.DisplayName;
					btnLink.Attributes["onclick"] = "document.location.href='" + myTab.URL + "';";
					btnLink.Attributes["class"] = spanCssClass;
					btnLink.ID = myTab.DisplayPosition.ToString();
					this.Controls.Add(new LiteralControl("<td class=\"" + tdCssClass + "\">"));
					this.Controls.Add(btnLink);
					this.Controls.Add(new LiteralControl("</td>"));

					//Right Image if necessary
					if (GetTabColspan() > 2) 
					{
						this.Controls.Add(new LiteralControl("<td class=\"" + tdCssClass + "\"><img src=\"" + rightImage + "\"></td>"));
					}
				}
				intFinish++;
			}

			this.Controls.Add(new LiteralControl("</tr>"));
		}

		//Returns render string for bottom image row
		private string BottomRow(int intStart,ref int intFinish, int intSelected) 
		{
			//intStart is the first tab index to display
			//intFinish is the last tab index displayed
			//intSelected is the currently selected tab

			int intTabspan = 0;
			string strReturn = "<tr>";

			//Set how many tabs to go across the page
			if (intTabsAcross <= items.Count) 
			{ intTabspan = intTabsAcross; }
			else { intTabspan = items.Count; }

			//Build up display string
			for (int i=0; i < intTabspan; ++i) 
			{
				if (items.Count > (intStart+i)) 
				{
					Tab myTab = (Tab)items[i+intStart];

					string bottomImage = (intSelected == myTab.DisplayPosition) ? strSelBottomImage : strBottomImage;

					strReturn += "<td colspan=\"" + GetTabColspan().ToString() + "\" class=\"" + cssClass + "\">";
					strReturn += "<img border=\"0\" src=\"" + bottomImage + "\">";
					strReturn += "</td>";
				}
				intFinish++;
			}

			strReturn += "</tr>";
			return strReturn;
		}

		//Build the control
		protected override void CreateChildControls() 
		{
			int intColRows = (items.Count / intTabsAcross) + 1;
			int intPositionRendered = 0;
			int intStartPosition = 0;

			order();

			this.Controls.Clear();

			this.Controls.Add(new LiteralControl("<table cellpadding=\"2\" cellspacing=\"1\" border=\"0\">"));

			for (int i=1; i <= intColRows; ++i) 
			{
				if ((strTopImage.Length > 0) || (strSelTopImage.Length > 0))
				{
					this.Controls.Add(new LiteralControl(TopRow(intStartPosition, ref intPositionRendered, intSelTab)));
				}
				MiddleRow(intStartPosition, ref intPositionRendered, intSelTab);
				if ((strBottomImage.Length > 0) || (strSelBottomImage.Length > 0)) 
				{
					this.Controls.Add(new LiteralControl(BottomRow(intStartPosition, ref intPositionRendered, intSelTab)));
				}
				intStartPosition = intPositionRendered;
			}

			this.Controls.Add(new LiteralControl("</table>"));

		}

		//Saves the View State
		protected override object SaveViewState() 
		{
			object[] allStates = new object[2];
			allStates[0] = base.SaveViewState();
			allStates[1] = intSelTab;
			return allStates;
		}
		//Loads View State Info																																																		  'Load Saved State Values
		protected override void LoadViewState(object savedState) 
		{
			if (savedState != null)
			{
				object[] myState = (object[])savedState;
				if (myState[0] != null) 
				{
					base.LoadViewState(myState[0]);
				}
				if (myState[1] != null) 
				{
					intSelTab = Convert.ToInt32(myState[1]);
				}
			}
		}
		#endregion

	}

	#endregion
}

